package com.liangzhong.modules.Main.Mediator
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.events.ActivityEvent;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.events.TaskEvent;
	import com.liangzhong.mediator.BaseMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.MatchType;
	import com.liangzhong.proxyconstant.activity.ActivityConst;
	import com.liangzhong.proxyconstant.task.TaskProxyConstant;
	import com.lz.football.module.activity.IVipWard;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.playerUp.IPlayerUpView;
	import com.lz.football.module.task.ITaskFinishView;
	
	import common.effect.IEffectView;
	import common.lvAward.ILvAwardView;
	import common.matchAward.IMatchAwardView;
	import common.playerUpgrade.IPlayerUpgradeView;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.setTimeout;
	
	import lz.controls.managers.AnimationManager;
	import lz.controls.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	
	/**
	 *特效效果 
	 * @author lake
	 * 
	 */	
	public class EffectMediator extends BaseMediator
	{
		/**
		 * MediatorName
		 */		
		public static const MediatorName :String = "EffectMediator";
		/**
		 *是否俱乐部有升级 
		 */		
		private var isClubUpGrade :Boolean = false;
		/**
		 *完成的任务列表 
		 */		
		private var ftaskList		:Array = [];
		
		
		/**
		 *任务 
		 */
		private var _iTaskFinishView:ITaskFinishView;
		
		public function EffectMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			AppEventDispatcher.getInstance().addEventListener(NotifierEvent.CLUB_LV_CHANGE,onClubLevHandler);
			AppEventDispatcher.getInstance().addEventListener(TaskEvent.TASK_FINISH_TASK,onTaskfinish_Track);
			AppData.getInstance().taskManager.addEventListener(TaskEvent.TASK_FINISH_TASK,onTaskfinish);
			AppEventDispatcher.getInstance().addEventListener(ActivityEvent.ACTIVITY_VIP_DAILY_AWARD,onAwardHandler);
			AppEventDispatcher.getInstance().addEventListener(ActivityEvent.ACTIVITY_LEVEL_AWARD,onLevelAwardHandler);
		}
		/**
		 * vip 奖励 
		 * @param event
		 * 
		 */		
		protected function onAwardHandler(event:ActivityEvent):void
		{
			this.sendNotification(ActivityConst.VIP_DAILY_AWARD,event.param);
		}
		/**
		 *等级奖励 
		 * @param event
		 * 
		 */		
		protected function onLevelAwardHandler(event:ActivityEvent):void
		{
			this.sendNotification(ActivityConst.LEVEL_AWARD,event.param);
		}
		/**
		 *通过点击任务条目完成 
		 * @param event
		 * 
		 */		
		private function onTaskfinish_Track(event:TaskEvent):void
		{
			ftaskList.splice(ftaskList.indexOf(event.param.vo),1);//删除预存的完成任务
			AnimationManager.addItem({func:taskfinish,data:event.param.vo});
		}
		/**
		 *完成任务 通过server 进度自动完成
		 * @param event
		 * 
		 */
		private function onTaskfinish(event:TaskEvent):void
		{
			var guideStep :int = AppData.getInstance().clubDatamanager.guideStep;
			if(AppData.getInstance().matchDataManager.getIsGameStartLen() > 0)//是否有比赛没关掉
			{
				ftaskList.push(event.param.vo);
				return;
			}
			if(AppData.getInstance().clubDatamanager.clubInfo.level == 2 && guideStep < 30)//引导(探索，球星放入阵容)
			{
				ftaskList.push(event.param.vo);
				return;
			}
			AnimationManager.addItem({func:taskfinish,data:event.param.vo});
		}
		/**
		 *任务完成 (点击界面导航)
		 * 
		 */
		private function taskfinish($vo:*):void
		{
			var taskFinish :IComponent = Reflection.createInstance("com.lz.football.module.task.view.TaskFinishView");
			if(taskFinish)
			{
				taskFinish.initUI();
				_iTaskFinishView = taskFinish as ITaskFinishView;
				_iTaskFinishView.setDate($vo);
				PopUpManager.addWindow(taskFinish as DisplayObject,GlobalDef.AlertContainer,(GlobalDef.StageWidth - 1027)>> 1,(GlobalDef.StageHeight - 203)>> 1);
			}
			else
			{
				AnimationManager.remove();
			}
		}
		/**
		 * 俱乐部升级
		 * @param e
		 * 
		 */		
		private function onClubLevHandler(e :Event):void
		{
			if(AppData.getInstance().matchDataManager.getIsGameStartLen() > 0)//是否有比赛没关掉
			{
				isClubUpGrade = true;
				return;
			}
			AnimationManager.addItem({func:clubLineUpEffect});
			var lev :int = AppData.getInstance().clubDatamanager.clubInfo.level;
			if(lev == 5 || lev == 10 || lev == 15)
			{
				AnimationManager.addItem({func:showLvAward});
			}
		}
		/**
		 *俱乐部升级效果 
		 */		
		private function clubLineUpEffect():void
		{
			var effectView :IEffectView = Reflection.createInstance("common.effect.club.ClubUpgradeEffectView");
			if(effectView)
			{
				PopUpManager.addWindow(effectView as DisplayObject,GlobalDef.GameStage,0,0,true);
				effectView.initUI();
			}
			else
			{
				AnimationManager.remove();
			}
		}
		/**
		 *显示球员升级效果 
		 */		
		private function showPlayerUpgradeEffect():void
		{
			if(AppData.getInstance().trainingCampDataManager.upPlayerArr.length == 0)
			{
				AnimationManager.remove();
				return;
			}
			var arr:Array = AppData.getInstance().trainingCampDataManager.upPlayerArr;
			var playerUpgradeView :IPlayerUpgradeView = Reflection.createInstance("common.playerUpgrade.view.PlayerUpgradeView");
			if(playerUpgradeView)
			{
				playerUpgradeView.initUI();
				playerUpgradeView.data = arr;
				PopUpManager.addWindow(playerUpgradeView as DisplayObject,GlobalDef.GameStage,(GlobalDef.StageWidth - 547)>>1,(GlobalDef.StageHeight - 489)>>1);
			}
			else
			{
				AnimationManager.remove();
			}
			
		}
		/**
		 *联赛效果 
		 */		
		private function leagueMatchEffect(value:*):void
		{
			var effectView :IEffectView = Reflection.createInstance("common.effect.match.LeagueMatchEffectView");
			if(effectView)
			{
				PopUpManager.addWindow(effectView as DisplayObject,GlobalDef.GameStage,0,0,true);
				effectView.initUI();
				effectView.data = value;
			}
			else
			{
				AnimationManager.remove();
			}
		}
		/**
		 *赛后抽奖 
		 * 
		 */		
		private function matchAward(objP:*):void
		{
			var matchAwardView :IMatchAwardView = Reflection.createInstance("common.matchAward.view.MatchAwardView");
			if(matchAwardView && objP)
			{
				matchAwardView.data = objP;
				matchAwardView.initUI();
				PopUpManager.addWindow(matchAwardView as DisplayObject,GlobalDef.GameStage,0,0,true);
			}
			else
			{
				AnimationManager.remove();
			}
		}
		/**
		 * 注册感兴趣的命令
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [NotificationConstants.PLAYER_UPGRADE,
				NotificationConstants.MATCH_END,
				NotificationConstants.MATCH_SHOW_AWARD,
				NotificationConstants.PLAY_EFFECT,
				NotificationConstants.APPEAR_FINISH_TASK,
				TaskProxyConstant.GET_GET_TASK_WARD,
				ActivityConst.VIP_LV_CHANGE,
				ActivityConst.LOGIN_VIP_DAILY_AWARD,
				ActivityConst.FINISH_FIRST_CHARGE,
				ActivityConst.GET_LEVEL_INFO];
		}
		/**
		 * 捕获通知
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case NotificationConstants.PLAYER_UPGRADE://球员升级
				{
					if(notification.getBody().show && AppData.getInstance().trainingCampDataManager.upPlayerArr.length > 0)
					{
						showPlayerUpgradeEffect();
						return;
					}
					if(AppData.getInstance().matchDataManager.getIsGameStartLen() > 0)//是否有比赛没关掉
					{
						return;
					}
					if(AppData.getInstance().trainingCampDataManager.upPlayerArr.length > 0)//是否有球员升级
					{
						AnimationManager.addItem({func:showPlayerUpgradeEffect});
					}
					break;
				}
				case NotificationConstants.MATCH_END://比赛结束 用户按结束按钮 关掉页面
				{
					showEffect();
					var matchType :int = int(notification.getBody().type);
					switch(matchType)
					{
						case MatchType.PRE_MATCH:
						case MatchType.BASE_MATCH:
						case MatchType.CHAMPION_MATCH:
						case MatchType.LEGEND_MATCH:
						{
							//判断是否有状态 成功 失败 
							var passStatus :int = AppData.getInstance().leagueMatchManager.passStatus;
							if(passStatus > 0)
							{
								AnimationManager.addItem({func:leagueMatchEffect,data:{type:passStatus}});
								AppData.getInstance().leagueMatchManager.passStatus = 0;
							}
							break;
						}
					}
					break;
				}
				case NotificationConstants.MATCH_SHOW_AWARD://赛后抽奖
				{
					AnimationManager.addItem({func:matchAward,data:notification.getBody()});
					break;
				}
				case NotificationConstants.PLAY_EFFECT://播放效果
				{
					showEffect();
					break;
				}
				case NotificationConstants.APPEAR_FINISH_TASK://完成任务
				{
					ftaskList = ftaskList.concat(notification.getBody() as Array);
					showEffect();
					break;
				}
				case TaskProxyConstant.GET_GET_TASK_WARD://返回领取任务奖励 
				{
					if(_iTaskFinishView != null)
					{
						_iTaskFinishView.getWard();
					}
					_iTaskFinishView = null;
					AnimationManager.remove();
					break;
				}
				case ActivityConst.VIP_LV_CHANGE://vip等级改变 
				{
					var viplv:int = AppData.getInstance().clubDatamanager.clubInfo.VIP;
					var tempLv:int = notification.getBody().lv;
					for(viplv;viplv < tempLv;viplv++)
					{
						AnimationManager.addItem({func:vipDailyWard,data:{lv:viplv + 1,isRequest:false}});
						AnimationManager.addItem({func:vipLvWard,data:{lv:viplv + 1}});
					}
//					AnimationManager.addItem({func:vipDailyWard,data:2});
//					AnimationManager.addItem({func:vipLvWard,data:2});
					break;
				}
				case ActivityConst.LOGIN_VIP_DAILY_AWARD://vip每日奖励
				{
					AnimationManager.addItem({func:vipDailyWard,data:{lv:AppData.getInstance().clubDatamanager.clubInfo.VIP,isRequest:true}});
					break;
				}
				case ActivityConst.FINISH_FIRST_CHARGE://首充奖励
				{
					AnimationManager.addItem({func:thefirstCharge});
					break;
				}
				case ActivityConst.GET_LEVEL_INFO://5级 10级 15级 奖励
				{
					var lev :int = AppData.getInstance().clubDatamanager.clubInfo.level;
					if(lev == 5 || lev == 10 || lev == 15)
					{
						AnimationManager.addItem({func:showLvAward});
					}
					break;
				}
			}
		}
		/**
		 *依次播放动画 
		 */		
		private function showEffect():void
		{
			if(AppData.getInstance().trainingCampDataManager.upPlayerArr.length > 0)//是否有球员升级
			{
				AnimationManager.addItem({func:showPlayerUpgradeEffect});
			}
			if(ftaskList.length > 0)
			{
				for each(var data :Object in ftaskList)
				{
					AnimationManager.addItem({func:taskfinish,data:data});
				}
				ftaskList.splice(0);
			}
			if(isClubUpGrade)
			{
				isClubUpGrade = false;
				AnimationManager.addItem({func:clubLineUpEffect});
				var lev :int = AppData.getInstance().clubDatamanager.clubInfo.level;
				if(lev == 5 || lev == 10 || lev == 15)
				{
					AnimationManager.addItem({func:showLvAward});
				}
			}
		}
		/**
		 *vip每日奖励动画  
		 * 
		 */
		private function vipDailyWard($obj:Object):void
		{
			var wardView:IComponent = Reflection.createInstance("com.lz.football.module.activity.vip.VipDailyWardView");
			wardView.initUI();
			(wardView as IVipWard).showWard($obj);
			PopUpManager.addWindow(wardView as DisplayObject,GlobalDef.AlertContainer,0,0,false);
		}
		/**
		 *vip等级奖励动画  
		 * 
		 */
		private function vipLvWard($obj:Object):void
		{
			var wardView:IComponent = Reflection.createInstance("com.lz.football.module.activity.vip.VipLvWardView");
			wardView.initUI();
			(wardView as IVipWard).showWard($obj);
			
			PopUpManager.addWindow(wardView as DisplayObject,GlobalDef.AlertContainer);
		}
		/**
		 *首充 
		 * 
		 */
		private function thefirstCharge():void
		{
			var wardView:IComponent = Reflection.createInstance("com.lz.football.module.activity.Thefirstcharge.TheFirstChargeWardView");
			wardView.initUI();
			
			PopUpManager.addWindow(wardView as DisplayObject,GlobalDef.AlertContainer);
		}
		/**
		 *显示等级奖励 
		 */		
		private function showLvAward():void
		{
			var lvObj :Object = AppData.getInstance().activityDatemanager.rushLevelObj;
			if(lvObj)
			{
				var clv :int = AppData.getInstance().clubDatamanager.clubInfo.level;
				var lvAwardView :ILvAwardView;
				if(clv == 5 && (lvObj.status5==0 || lvObj.status5==1))
				{
					lvAwardView = Reflection.createInstance("common.lvAward.LvAwardView");
					lvAwardView.initUI();
					lvAwardView.showView(5);
					PopUpManager.addPopUp(lvAwardView as DisplayObject,GlobalDef.AlertContainer,GlobalDef.StageWidth,GlobalDef.StageHeight,true);
				}
				else if(clv == 10 && (lvObj.status6==0 || lvObj.status6==1))
				{
					lvAwardView = Reflection.createInstance("common.lvAward.LvAwardView");
					lvAwardView.initUI();
					lvAwardView.showView(6);
					PopUpManager.addPopUp(lvAwardView as DisplayObject,GlobalDef.AlertContainer,GlobalDef.StageWidth,GlobalDef.StageHeight,true);
				}
				else if(clv == 15 && (lvObj.status7==0 || lvObj.status7==1))
				{
					lvAwardView = Reflection.createInstance("common.lvAward.LvAwardView");
					lvAwardView.initUI();
					lvAwardView.showView(7);
					PopUpManager.addPopUp(lvAwardView as DisplayObject,GlobalDef.AlertContainer,GlobalDef.StageWidth,GlobalDef.StageHeight,true);
				}
				else
				{
					AnimationManager.remove();
				}
			}
			else
			{
				AnimationManager.remove();
			}
		}
		/**
		 * 
		 * 注册进行时
		 */		
		override public function onRegister():void
		{
			addEventListeners();
			//setTimeout(clubLineUpEffect,3000);
			//setTimeout(delayTest,3000);
		}
		/**
		 *测试 
		 */		
		private function delayTest():void
		{
			//AnimationManager.addItem({func:leagueMatchEffect,data:{type:3}});
			//this.sendNotification(ApplicationConstants.Login_User_Get_Sign_Award,2);
			//this.sendNotification(ApplicationConstants.Login_User_Get_Sign_Award,3);
			//this.sendNotification(ApplicationConstants.Login_User_Get_Sign_Award,4);
		}
	}
}